package com.oracle.controller;

import com.oracle.vo.Item;
import com.oracle.vo.Order;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

@RestController
@RequestMapping("/order")
public class OrederController {

    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping("/make")
    public Order makeOrder(String userName,String itemIds){
//        用来存储远程调用的商品信息
        List<Item> list = new ArrayList<>();

        Order order = new Order();
        order.setiOrderNo(new Random().nextInt(1000000));
        order.setsUserName(userName);

        //远程调用商品服务
        String[] split = itemIds.split(",");
        for(String id:split){

        Item item = restTemplate.getForObject("http://localhost:8001/item/get?itemNo={itemNo}", Item.class,id);

        list.add(item);
        }
        order.setlItems(list);
        return order;
    }

    @RequestMapping("/order_sava")
    public String OrderSave(Item item){
        String s = restTemplate.postForObject("http://localhost:8001/item/saveItem",item,String.class,item);
        return s;
    }
}
